﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Utilities;
using NHibernate;

namespace HiTeachers.Data
{
    public class TeacherDao : AbstractNHibernateDao<Teacher, Guid>, ITeacherDao
    {
        public Teacher IsEmailAlreadyExist(string emailAddress)
        {
            try
            {
                const string hsql = "from Teacher t where t.Email = :emailAddress";

                IQuery query = NHibernateSession.CreateQuery(hsql);
                query.SetString("emailAddress", emailAddress);

                return query.UniqueResult<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return null;
            }
        }

        public IList<Teacher> GetNewestTeachers(int maxResult)
        {
            try
            {
                const string hsql = "from Teacher t where t.Email != 'administrator@hiteachers.com' and t.IsVerified = 1 order by t.RegistedDate desc";

                var query = NHibernateSession.CreateQuery(hsql);

                query.SetMaxResults(maxResult);
                query.SetFirstResult(0);

                return query.List<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<Teacher>();
            }
        }

        public IList<Teacher> GetSubcribeTeachers(string subjects)
        {
            try
            {
                string hsql = "from Teacher t where t.IsVerified = 1 and t.IsReceiveNews = 1 and (";
                string[] subs = subjects.Split(',');
                for (int i = 0; i < subs.Length; i++)
                {
                    string sub = subs[i];
                    if(i ==0) hsql += string.Format(" t.Subject like '%{0}%' ", sub);
                    else hsql += string.Format(" or t.Subject like '%{0}%' ", sub);
                }
                if (subs.Length > 0) hsql += ")";
                var query = NHibernateSession.CreateQuery(hsql);

                return query.List<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<Teacher>();
            }
        }

        public int TotalActivateTeachers()
        {
            try
            {
                const string hsql = "select count(*) from Teacher t where t.Email != 'administrator@hiteachers.com' and t.IsVerified = 1";

                var query = NHibernateSession.CreateQuery(hsql);

                return Convert.ToInt32(query.UniqueResult());
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return 0;
            }
        }

        public IList<Teacher> GetActivateTeachers(int maxResult, int startIndex)
        {
            try
            {
                const string hsql = "from Teacher t where t.Email != 'administrator@hiteachers.com' and t.IsVerified = 1";

                var query = NHibernateSession.CreateQuery(hsql);

                query.SetMaxResults(maxResult);
                query.SetFirstResult(startIndex);

                return query.List<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<Teacher>();
            }
        }

        public int TotalNumberOfTeacher(int accountStatus)
        {
            try
            {
                var hsql = "select count(*) from Teacher t where t.Email != 'administrator@hiteachers.com'";

                if (accountStatus != EntityType.AllMembers) hsql += " and t.IsFullAccount = :fullAcount";

                var query = NHibernateSession.CreateQuery(hsql);
                if (accountStatus != EntityType.AllMembers) query.SetBoolean("fullAcount", (accountStatus == EntityType.FullMember));

                return Convert.ToInt32(query.UniqueResult());
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return 0;
            }
        }

        public IList<Teacher> GetTeachers(int accountStatus)
        {
            try
            {
                var hsql = "from Teacher t where t.Email != 'administrator@hiteachers.com'";
                if (accountStatus != EntityType.AllMembers) hsql += " and t.IsFullAccount = :fullAcount";

                var query = NHibernateSession.CreateQuery(hsql);
                if (accountStatus != EntityType.AllMembers) query.SetBoolean("fullAcount", (accountStatus == EntityType.FullMember));

                return query.List<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<Teacher>();
            }
        }

        public IList<Teacher> GetTeachers(int accountStatus, int maximumRows, int startRowIndex)
        {
            try
            {
                var hsql = "from Teacher t where t.Email != 'administrator@hiteachers.com'";
                if (accountStatus != EntityType.AllMembers) hsql += " and t.IsFullAccount = :fullAcount";

                var query = NHibernateSession.CreateQuery(hsql);
                if (accountStatus != EntityType.AllMembers) query.SetBoolean("fullAcount", (accountStatus == EntityType.FullMember));

                query.SetMaxResults(maximumRows);
                query.SetFirstResult(startRowIndex);

                return query.List<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<Teacher>();
            }
        }

        public Teacher GetUser(string username)
        {
            try
            {
                const string hsql = "from Teacher t where t.Email = :emailAddress";

                IQuery query = NHibernateSession.CreateQuery(hsql);
                query.SetString("emailAddress", username);

                return query.UniqueResult<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return null;
            }
        }

        public Teacher GetUser(string username, string password)
        {
            try
            {
                const string hsql = "from Teacher t where t.Email = :emailAddress and t.Password = :password";

                IQuery query = NHibernateSession.CreateQuery(hsql);
                query.SetString("emailAddress", username);
                query.SetString("password", password);

                return query.UniqueResult<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return null;
            }
        }

        public IList<Teacher> GetRemindMembers()
        {
            try
            {
                const string hsql = "from Teacher t where t.IsFullAccount = 1 and t.DeactivatedDate = :deAtcDate";

                IQuery query = NHibernateSession.CreateQuery(hsql);
                query.SetDateTime("deActDate", DateTime.Now.AddDays(7).Date);

                return query.List<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<Teacher>();
            }
        }

        public IList<Teacher> GetExpiedMembers()
        {
            try
            {
                const string hsql = "from Teacher t where t.IsFullAccount = 1 and t.DeactivatedDate = :deAtcDate";

                IQuery query = NHibernateSession.CreateQuery(hsql);
                query.SetDateTime("deAtcDate", DateTime.Now.AddDays(-1).Date);

                return query.List<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<Teacher>();
            }
        }
    }
}
